package org.paul.library.utils;

import android.text.TextUtils;
import android.util.Log;

public class Logger {

    private static final int tagMaxLength = 30;
    private static final int msgMaxLength=3*1024;
    private static String format = "[%-30s]";

    /**
     * 日志使能开关
     */
    private boolean enable;

    public boolean isEnable() {
        return enable;
    }

    public void setEnable(boolean enable) {
        this.enable = enable;
    }

    public static void logD(String tag, String msg){
        Log.d(formatTag(tag),formatMsg(msg));
    }


    private static String formatMsg(String msg) {
        if(msg.length()>msgMaxLength){
            //分批打印
        }

    }


    public static java.util.logging.Logger getLogger(String tag) {
        return java.util.logging.Logger.getLogger(formatTag(tag));
    }

    private static String formatTag(String tag) {
        if (!TextUtils.isEmpty(tag)) {
            int length = tag.length();
            if (length > tagMaxLength) {
                tag = subTag(tag);
            }
        }
        return String.format(Logger.format, tag);
    }

    private static String subTag(String tag) {
        String[] split = tag.split("\\.");
        StringBuilder stringBuilder = new StringBuilder();
        for (String s : split) {
            CharSequence charSequence = s.subSequence(0, 1);
            stringBuilder.append(charSequence).append(".");
        }
        String s = stringBuilder.toString();
        if (s.endsWith(".")) {
            s = stringBuilder.deleteCharAt(s.lastIndexOf(".")).toString();
        }
        return s;
    }

}
